{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "04b75f0f",
   "metadata": {},
   "source": [
    "# 导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f0a816c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T14:52:20.656976Z",
     "start_time": "2023-11-19T14:52:20.645804Z"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import json\n",
    "import joblib\n",
    "import logging\n",
    "import warnings\n",
    "import pandas as pd\n",
    "from typing import List\n",
    "from skopt import BayesSearchCV\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.compose import ColumnTransformer\n",
    "from sklearn.pipeline import Pipeline\n",
    "from lightgbm import LGBMClassifier\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "pd.set_option(\"display.width\", 10000)\n",
    "pd.set_option(\"display.max_rows\", None)\n",
    "pd.set_option(\"display.max_columns\", None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "973dd029",
   "metadata": {},
   "source": [
    "# 加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "78b6e6a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T11:54:33.113302Z",
     "start_time": "2023-11-19T11:54:33.034385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>age</th>\n",
       "      <th>job</th>\n",
       "      <th>marital</th>\n",
       "      <th>education</th>\n",
       "      <th>default</th>\n",
       "      <th>housing</th>\n",
       "      <th>loan</th>\n",
       "      <th>contact</th>\n",
       "      <th>month</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>duration</th>\n",
       "      <th>campaign</th>\n",
       "      <th>pdays</th>\n",
       "      <th>previous</th>\n",
       "      <th>poutcome</th>\n",
       "      <th>emp_var_rate</th>\n",
       "      <th>cons_price_index</th>\n",
       "      <th>cons_conf_index</th>\n",
       "      <th>lending_rate3m</th>\n",
       "      <th>nr_employed</th>\n",
       "      <th>subscribe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>51</td>\n",
       "      <td>admin.</td>\n",
       "      <td>divorced</td>\n",
       "      <td>professional.course</td>\n",
       "      <td>no</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>aug</td>\n",
       "      <td>mon</td>\n",
       "      <td>4621</td>\n",
       "      <td>1</td>\n",
       "      <td>112</td>\n",
       "      <td>2</td>\n",
       "      <td>failure</td>\n",
       "      <td>1.4</td>\n",
       "      <td>90.81</td>\n",
       "      <td>-35.53</td>\n",
       "      <td>0.69</td>\n",
       "      <td>5219.74</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>50</td>\n",
       "      <td>services</td>\n",
       "      <td>married</td>\n",
       "      <td>high.school</td>\n",
       "      <td>unknown</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>may</td>\n",
       "      <td>mon</td>\n",
       "      <td>4715</td>\n",
       "      <td>1</td>\n",
       "      <td>412</td>\n",
       "      <td>2</td>\n",
       "      <td>nonexistent</td>\n",
       "      <td>-1.8</td>\n",
       "      <td>96.33</td>\n",
       "      <td>-40.58</td>\n",
       "      <td>4.05</td>\n",
       "      <td>4974.79</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>48</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>divorced</td>\n",
       "      <td>basic.9y</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>apr</td>\n",
       "      <td>wed</td>\n",
       "      <td>171</td>\n",
       "      <td>0</td>\n",
       "      <td>1027</td>\n",
       "      <td>1</td>\n",
       "      <td>failure</td>\n",
       "      <td>-1.8</td>\n",
       "      <td>96.33</td>\n",
       "      <td>-44.74</td>\n",
       "      <td>1.50</td>\n",
       "      <td>5022.61</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>26</td>\n",
       "      <td>entrepreneur</td>\n",
       "      <td>single</td>\n",
       "      <td>high.school</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>aug</td>\n",
       "      <td>fri</td>\n",
       "      <td>359</td>\n",
       "      <td>26</td>\n",
       "      <td>998</td>\n",
       "      <td>0</td>\n",
       "      <td>nonexistent</td>\n",
       "      <td>1.4</td>\n",
       "      <td>97.08</td>\n",
       "      <td>-35.55</td>\n",
       "      <td>5.11</td>\n",
       "      <td>5222.87</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>45</td>\n",
       "      <td>admin.</td>\n",
       "      <td>single</td>\n",
       "      <td>university.degree</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>nov</td>\n",
       "      <td>tue</td>\n",
       "      <td>3178</td>\n",
       "      <td>1</td>\n",
       "      <td>240</td>\n",
       "      <td>4</td>\n",
       "      <td>success</td>\n",
       "      <td>-3.4</td>\n",
       "      <td>89.82</td>\n",
       "      <td>-33.83</td>\n",
       "      <td>1.17</td>\n",
       "      <td>4884.70</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  age           job   marital            education  default housing loan   contact month day_of_week  duration  campaign  pdays  previous     poutcome  emp_var_rate  cons_price_index  cons_conf_index  lending_rate3m  nr_employed subscribe\n",
       "0   1   51        admin.  divorced  professional.course       no     yes  yes  cellular   aug         mon      4621         1    112         2      failure           1.4             90.81           -35.53            0.69      5219.74        no\n",
       "1   2   50      services   married          high.school  unknown     yes   no  cellular   may         mon      4715         1    412         2  nonexistent          -1.8             96.33           -40.58            4.05      4974.79       yes\n",
       "2   3   48   blue-collar  divorced             basic.9y       no      no   no  cellular   apr         wed       171         0   1027         1      failure          -1.8             96.33           -44.74            1.50      5022.61        no\n",
       "3   4   26  entrepreneur    single          high.school      yes     yes  yes  cellular   aug         fri       359        26    998         0  nonexistent           1.4             97.08           -35.55            5.11      5222.87       yes\n",
       "4   5   45        admin.    single    university.degree       no      no   no  cellular   nov         tue      3178         1    240         4      success          -3.4             89.82           -33.83            1.17      4884.70        no"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv(\"../dataset/train.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0afaa164",
   "metadata": {},
   "source": [
    "# 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ca487210",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T11:54:35.428483Z",
     "start_time": "2023-11-19T11:54:35.204439Z"
    }
   },
   "outputs": [],
   "source": [
    "# 列出需要标准化的数值型特征和需要独热编码的类别型特征\n",
    "numeric_features = ['age', 'duration', 'campaign', 'pdays', 'previous', 'emp_var_rate', \n",
    "                    'cons_price_index', 'cons_conf_index', 'lending_rate3m', 'nr_employed']\n",
    "categorical_features = ['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'day_of_week', 'poutcome']\n",
    "# 定义ColumnTransformer\n",
    "preprocessor = ColumnTransformer(\n",
    "    transformers=[\n",
    "        ('poly', PolynomialFeatures(degree=4, include_bias=False), numeric_features),  # 标准化数值型特征\n",
    "        ('cat', OneHotEncoder(), categorical_features)  # 独热编码类别型特征\n",
    "    ])\n",
    "\n",
    "# 定义包含数据处理和特征工程的Pipeline\n",
    "preprocessing_pipeline = Pipeline([\n",
    "    ('preprocessor', preprocessor)  # 数据处理和特征工程\n",
    "])\n",
    "\n",
    "# 在训练集上拟合数据处理和特征工程的Pipeline\n",
    "X_transformed = preprocessing_pipeline.fit_transform(train)\n",
    "\n",
    "y = train[\"subscribe\"].apply(lambda x: 1 if x == \"yes\" else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "267d726d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T11:54:37.151828Z",
     "start_time": "2023-11-19T11:54:37.128951Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['../preprocessing_pipeline.pkl']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(preprocessing_pipeline, \"../preprocessing_pipeline.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f336a187",
   "metadata": {},
   "source": [
    "# 贝叶斯搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "853c2e98",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T13:24:47.818515Z",
     "start_time": "2023-11-19T11:54:40.159230Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] Unknown parameter: class_weight_ratio\n",
      "Best parameters found:  OrderedDict([('class_weight_ratio', 7), ('colsample_bytree', 0.8023081537462333), ('importance_type', 'split'), ('learning_rate', 0.019998641631705356), ('max_depth', 7), ('min_child_samples', 20), ('min_child_weight', 7.787166513959646), ('min_split_gain', 0.5390892508171149), ('n_estimators', 333), ('num_leaves', 155), ('reg_alpha', 0.7036459700774684), ('reg_lambda', 0.9016214271191245), ('subsample', 1.0), ('subsample_for_bin', 41379), ('subsample_freq', 1)])\n",
      "Best cross-validation score: -0.8903\n",
      "CPU times: total: 22min 36s\n",
      "Wall time: 1h 30min 7s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# 定义参数搜索范围\n",
    "param_space = {\n",
    "    'num_leaves': (5, 200),\n",
    "    'max_depth': (3, 15),\n",
    "    'learning_rate': (0.01, 0.1, 'log-uniform'),\n",
    "    'n_estimators': (50, 500),\n",
    "    'subsample_for_bin': (20000, 300000),\n",
    "    'class_weight_ratio': (1, 10),\n",
    "    'min_split_gain': (0.0, 1.0),\n",
    "    'min_child_weight': (0.5, 10),\n",
    "    'min_child_samples': (5, 20),\n",
    "    'subsample': (0.5, 1.0),\n",
    "    'subsample_freq': (1, 10),\n",
    "    'colsample_bytree': (0.5, 1.0),\n",
    "    'reg_alpha': (0.0, 1.0),\n",
    "    'reg_lambda': (0.0, 1.0),\n",
    "    'importance_type': ['split', 'gain']\n",
    "}\n",
    "\n",
    "lgbm_model = LGBMClassifier(boosting_type=\"gbdt\", objective=\"binary\", random_state=12)\n",
    "\n",
    "def objective_function(params):\n",
    "    # 将权重比例转换为字典形式\n",
    "    class_weight = {0: 1, 1: params['class_weight_ratio']}\n",
    "    params.pop('class_weight_ratio')\n",
    "    lgbm_model.set_params(class_weight=class_weight, **params)\n",
    "    return -np.mean(cross_val_score(lgbm_model, X_transformed, y, cv=5, scoring='roc_auc'))\n",
    "\n",
    "# 使用BayesSearchCV进行贝叶斯调参\n",
    "bayes_search = BayesSearchCV(\n",
    "    lgbm_model,\n",
    "    param_space,\n",
    "    n_iter=100,\n",
    "    cv=5,\n",
    "    scoring='roc_auc',\n",
    "    random_state=12,\n",
    "    n_jobs=-1\n",
    ")\n",
    "\n",
    "# 执行贝叶斯搜索\n",
    "bayes_search.fit(X_transformed, y)\n",
    "\n",
    "# 输出最佳参数\n",
    "print(\"Best parameters found: \", bayes_search.best_params_)\n",
    "\n",
    "# 输出最佳交叉验证分数\n",
    "print(\"Best cross-validation score: {:.4f}\".format(-bayes_search.best_score_))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daf442a2",
   "metadata": {},
   "source": [
    "# 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4bdd6957",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T14:49:12.529302Z",
     "start_time": "2023-11-19T14:49:04.296773Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\ttraining's binary_logloss: 0.383879\ttraining's auc: 0.88821\tvalid_1's binary_logloss: 0.380204\tvalid_1's auc: 0.854273\n",
      "[2]\ttraining's binary_logloss: 0.37815\ttraining's auc: 0.898361\tvalid_1's binary_logloss: 0.375209\tvalid_1's auc: 0.858519\n",
      "[3]\ttraining's binary_logloss: 0.372961\ttraining's auc: 0.901436\tvalid_1's binary_logloss: 0.370586\tvalid_1's auc: 0.865249\n",
      "[4]\ttraining's binary_logloss: 0.368057\ttraining's auc: 0.903737\tvalid_1's binary_logloss: 0.366312\tvalid_1's auc: 0.868396\n",
      "[5]\ttraining's binary_logloss: 0.363303\ttraining's auc: 0.905719\tvalid_1's binary_logloss: 0.362198\tvalid_1's auc: 0.869127\n",
      "[6]\ttraining's binary_logloss: 0.358771\ttraining's auc: 0.906803\tvalid_1's binary_logloss: 0.35833\tvalid_1's auc: 0.868884\n",
      "[7]\ttraining's binary_logloss: 0.354511\ttraining's auc: 0.907477\tvalid_1's binary_logloss: 0.354619\tvalid_1's auc: 0.869628\n",
      "[8]\ttraining's binary_logloss: 0.350411\ttraining's auc: 0.907676\tvalid_1's binary_logloss: 0.351164\tvalid_1's auc: 0.869214\n",
      "[9]\ttraining's binary_logloss: 0.346519\ttraining's auc: 0.909466\tvalid_1's binary_logloss: 0.347794\tvalid_1's auc: 0.871977\n",
      "[10]\ttraining's binary_logloss: 0.342774\ttraining's auc: 0.910141\tvalid_1's binary_logloss: 0.344711\tvalid_1's auc: 0.872666\n",
      "[11]\ttraining's binary_logloss: 0.3392\ttraining's auc: 0.910689\tvalid_1's binary_logloss: 0.341705\tvalid_1's auc: 0.872688\n",
      "[12]\ttraining's binary_logloss: 0.335829\ttraining's auc: 0.911102\tvalid_1's binary_logloss: 0.339053\tvalid_1's auc: 0.872838\n",
      "[13]\ttraining's binary_logloss: 0.332473\ttraining's auc: 0.912738\tvalid_1's binary_logloss: 0.336338\tvalid_1's auc: 0.873836\n",
      "[14]\ttraining's binary_logloss: 0.329307\ttraining's auc: 0.913677\tvalid_1's binary_logloss: 0.333781\tvalid_1's auc: 0.874613\n",
      "[15]\ttraining's binary_logloss: 0.326182\ttraining's auc: 0.914548\tvalid_1's binary_logloss: 0.331177\tvalid_1's auc: 0.875258\n",
      "[16]\ttraining's binary_logloss: 0.323214\ttraining's auc: 0.915324\tvalid_1's binary_logloss: 0.328807\tvalid_1's auc: 0.87569\n",
      "[17]\ttraining's binary_logloss: 0.320457\ttraining's auc: 0.915805\tvalid_1's binary_logloss: 0.326562\tvalid_1's auc: 0.875871\n",
      "[18]\ttraining's binary_logloss: 0.317677\ttraining's auc: 0.916751\tvalid_1's binary_logloss: 0.324334\tvalid_1's auc: 0.876449\n",
      "[19]\ttraining's binary_logloss: 0.315035\ttraining's auc: 0.917352\tvalid_1's binary_logloss: 0.322282\tvalid_1's auc: 0.876631\n",
      "[20]\ttraining's binary_logloss: 0.312488\ttraining's auc: 0.9179\tvalid_1's binary_logloss: 0.320293\tvalid_1's auc: 0.87698\n",
      "[21]\ttraining's binary_logloss: 0.310012\ttraining's auc: 0.918282\tvalid_1's binary_logloss: 0.318305\tvalid_1's auc: 0.876979\n",
      "[22]\ttraining's binary_logloss: 0.307492\ttraining's auc: 0.919104\tvalid_1's binary_logloss: 0.316296\tvalid_1's auc: 0.877453\n",
      "[23]\ttraining's binary_logloss: 0.305175\ttraining's auc: 0.91954\tvalid_1's binary_logloss: 0.314517\tvalid_1's auc: 0.877425\n",
      "[24]\ttraining's binary_logloss: 0.302809\ttraining's auc: 0.920151\tvalid_1's binary_logloss: 0.312812\tvalid_1's auc: 0.877577\n",
      "[25]\ttraining's binary_logloss: 0.300536\ttraining's auc: 0.92067\tvalid_1's binary_logloss: 0.311147\tvalid_1's auc: 0.877938\n",
      "[26]\ttraining's binary_logloss: 0.298402\ttraining's auc: 0.92098\tvalid_1's binary_logloss: 0.30952\tvalid_1's auc: 0.878219\n",
      "[27]\ttraining's binary_logloss: 0.296378\ttraining's auc: 0.921231\tvalid_1's binary_logloss: 0.30804\tvalid_1's auc: 0.878133\n",
      "[28]\ttraining's binary_logloss: 0.29433\ttraining's auc: 0.921698\tvalid_1's binary_logloss: 0.306479\tvalid_1's auc: 0.878425\n",
      "[29]\ttraining's binary_logloss: 0.29233\ttraining's auc: 0.922135\tvalid_1's binary_logloss: 0.305039\tvalid_1's auc: 0.878876\n",
      "[30]\ttraining's binary_logloss: 0.29038\ttraining's auc: 0.922544\tvalid_1's binary_logloss: 0.303659\tvalid_1's auc: 0.878773\n",
      "[31]\ttraining's binary_logloss: 0.288524\ttraining's auc: 0.923014\tvalid_1's binary_logloss: 0.302273\tvalid_1's auc: 0.878846\n",
      "[32]\ttraining's binary_logloss: 0.286709\ttraining's auc: 0.923363\tvalid_1's binary_logloss: 0.300957\tvalid_1's auc: 0.879086\n",
      "[33]\ttraining's binary_logloss: 0.284893\ttraining's auc: 0.92384\tvalid_1's binary_logloss: 0.299626\tvalid_1's auc: 0.879499\n",
      "[34]\ttraining's binary_logloss: 0.283148\ttraining's auc: 0.924294\tvalid_1's binary_logloss: 0.298376\tvalid_1's auc: 0.879679\n",
      "[35]\ttraining's binary_logloss: 0.281473\ttraining's auc: 0.924636\tvalid_1's binary_logloss: 0.297172\tvalid_1's auc: 0.879766\n",
      "[36]\ttraining's binary_logloss: 0.279846\ttraining's auc: 0.924926\tvalid_1's binary_logloss: 0.295983\tvalid_1's auc: 0.879875\n",
      "[37]\ttraining's binary_logloss: 0.278275\ttraining's auc: 0.925244\tvalid_1's binary_logloss: 0.294851\tvalid_1's auc: 0.880239\n",
      "[38]\ttraining's binary_logloss: 0.276776\ttraining's auc: 0.925503\tvalid_1's binary_logloss: 0.293825\tvalid_1's auc: 0.88038\n",
      "[39]\ttraining's binary_logloss: 0.27524\ttraining's auc: 0.925931\tvalid_1's binary_logloss: 0.292748\tvalid_1's auc: 0.880522\n",
      "[40]\ttraining's binary_logloss: 0.273757\ttraining's auc: 0.926174\tvalid_1's binary_logloss: 0.291649\tvalid_1's auc: 0.880708\n",
      "[41]\ttraining's binary_logloss: 0.272316\ttraining's auc: 0.926511\tvalid_1's binary_logloss: 0.290665\tvalid_1's auc: 0.881047\n",
      "[42]\ttraining's binary_logloss: 0.270929\ttraining's auc: 0.926748\tvalid_1's binary_logloss: 0.289673\tvalid_1's auc: 0.881353\n",
      "[43]\ttraining's binary_logloss: 0.269591\ttraining's auc: 0.927\tvalid_1's binary_logloss: 0.288766\tvalid_1's auc: 0.88146\n",
      "[44]\ttraining's binary_logloss: 0.268283\ttraining's auc: 0.927268\tvalid_1's binary_logloss: 0.287858\tvalid_1's auc: 0.88166\n",
      "[45]\ttraining's binary_logloss: 0.266957\ttraining's auc: 0.927749\tvalid_1's binary_logloss: 0.286982\tvalid_1's auc: 0.881852\n",
      "[46]\ttraining's binary_logloss: 0.265616\ttraining's auc: 0.928124\tvalid_1's binary_logloss: 0.286141\tvalid_1's auc: 0.882011\n",
      "[47]\ttraining's binary_logloss: 0.264395\ttraining's auc: 0.928406\tvalid_1's binary_logloss: 0.285274\tvalid_1's auc: 0.882187\n",
      "[48]\ttraining's binary_logloss: 0.263114\ttraining's auc: 0.928893\tvalid_1's binary_logloss: 0.284517\tvalid_1's auc: 0.882317\n",
      "[49]\ttraining's binary_logloss: 0.26194\ttraining's auc: 0.929321\tvalid_1's binary_logloss: 0.283807\tvalid_1's auc: 0.882422\n",
      "[50]\ttraining's binary_logloss: 0.260787\ttraining's auc: 0.929574\tvalid_1's binary_logloss: 0.283051\tvalid_1's auc: 0.882584\n",
      "[51]\ttraining's binary_logloss: 0.259628\ttraining's auc: 0.929885\tvalid_1's binary_logloss: 0.282364\tvalid_1's auc: 0.88273\n",
      "[52]\ttraining's binary_logloss: 0.258526\ttraining's auc: 0.93014\tvalid_1's binary_logloss: 0.281603\tvalid_1's auc: 0.882937\n",
      "[53]\ttraining's binary_logloss: 0.257424\ttraining's auc: 0.930455\tvalid_1's binary_logloss: 0.280929\tvalid_1's auc: 0.882967\n",
      "[54]\ttraining's binary_logloss: 0.256366\ttraining's auc: 0.930735\tvalid_1's binary_logloss: 0.280296\tvalid_1's auc: 0.883034\n",
      "[55]\ttraining's binary_logloss: 0.255325\ttraining's auc: 0.931092\tvalid_1's binary_logloss: 0.279696\tvalid_1's auc: 0.883118\n",
      "[56]\ttraining's binary_logloss: 0.254283\ttraining's auc: 0.931397\tvalid_1's binary_logloss: 0.279032\tvalid_1's auc: 0.883243\n",
      "[57]\ttraining's binary_logloss: 0.253277\ttraining's auc: 0.931657\tvalid_1's binary_logloss: 0.2784\tvalid_1's auc: 0.88338\n",
      "[58]\ttraining's binary_logloss: 0.252241\ttraining's auc: 0.93204\tvalid_1's binary_logloss: 0.277849\tvalid_1's auc: 0.883399\n",
      "[59]\ttraining's binary_logloss: 0.251217\ttraining's auc: 0.932357\tvalid_1's binary_logloss: 0.277306\tvalid_1's auc: 0.883469\n",
      "[60]\ttraining's binary_logloss: 0.250233\ttraining's auc: 0.93267\tvalid_1's binary_logloss: 0.276763\tvalid_1's auc: 0.883506\n",
      "[61]\ttraining's binary_logloss: 0.249257\ttraining's auc: 0.932944\tvalid_1's binary_logloss: 0.276264\tvalid_1's auc: 0.883569\n",
      "[62]\ttraining's binary_logloss: 0.248355\ttraining's auc: 0.933155\tvalid_1's binary_logloss: 0.275728\tvalid_1's auc: 0.883732\n",
      "[63]\ttraining's binary_logloss: 0.247373\ttraining's auc: 0.93349\tvalid_1's binary_logloss: 0.275325\tvalid_1's auc: 0.883684\n",
      "[64]\ttraining's binary_logloss: 0.24651\ttraining's auc: 0.933665\tvalid_1's binary_logloss: 0.274837\tvalid_1's auc: 0.883716\n",
      "[65]\ttraining's binary_logloss: 0.245566\ttraining's auc: 0.933971\tvalid_1's binary_logloss: 0.274455\tvalid_1's auc: 0.883684\n",
      "[66]\ttraining's binary_logloss: 0.244735\ttraining's auc: 0.934168\tvalid_1's binary_logloss: 0.273982\tvalid_1's auc: 0.883794\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[67]\ttraining's binary_logloss: 0.243866\ttraining's auc: 0.934423\tvalid_1's binary_logloss: 0.273478\tvalid_1's auc: 0.883968\n",
      "[68]\ttraining's binary_logloss: 0.243046\ttraining's auc: 0.93471\tvalid_1's binary_logloss: 0.273021\tvalid_1's auc: 0.884087\n",
      "[69]\ttraining's binary_logloss: 0.242176\ttraining's auc: 0.935017\tvalid_1's binary_logloss: 0.272635\tvalid_1's auc: 0.883963\n",
      "[70]\ttraining's binary_logloss: 0.241311\ttraining's auc: 0.935331\tvalid_1's binary_logloss: 0.27221\tvalid_1's auc: 0.884078\n",
      "[71]\ttraining's binary_logloss: 0.240477\ttraining's auc: 0.935559\tvalid_1's binary_logloss: 0.27189\tvalid_1's auc: 0.884075\n",
      "[72]\ttraining's binary_logloss: 0.239644\ttraining's auc: 0.935812\tvalid_1's binary_logloss: 0.271505\tvalid_1's auc: 0.884026\n",
      "[73]\ttraining's binary_logloss: 0.238899\ttraining's auc: 0.936028\tvalid_1's binary_logloss: 0.271102\tvalid_1's auc: 0.884066\n",
      "[74]\ttraining's binary_logloss: 0.23808\ttraining's auc: 0.936286\tvalid_1's binary_logloss: 0.270745\tvalid_1's auc: 0.884141\n",
      "[75]\ttraining's binary_logloss: 0.237347\ttraining's auc: 0.936457\tvalid_1's binary_logloss: 0.270324\tvalid_1's auc: 0.884229\n",
      "[76]\ttraining's binary_logloss: 0.236529\ttraining's auc: 0.936756\tvalid_1's binary_logloss: 0.269977\tvalid_1's auc: 0.884268\n",
      "[77]\ttraining's binary_logloss: 0.235759\ttraining's auc: 0.937053\tvalid_1's binary_logloss: 0.269664\tvalid_1's auc: 0.88425\n",
      "[78]\ttraining's binary_logloss: 0.23507\ttraining's auc: 0.937263\tvalid_1's binary_logloss: 0.26936\tvalid_1's auc: 0.884245\n",
      "[79]\ttraining's binary_logloss: 0.234284\ttraining's auc: 0.937545\tvalid_1's binary_logloss: 0.269063\tvalid_1's auc: 0.884252\n",
      "[80]\ttraining's binary_logloss: 0.233577\ttraining's auc: 0.937818\tvalid_1's binary_logloss: 0.268817\tvalid_1's auc: 0.88411\n",
      "[81]\ttraining's binary_logloss: 0.232898\ttraining's auc: 0.93806\tvalid_1's binary_logloss: 0.268535\tvalid_1's auc: 0.884123\n",
      "[82]\ttraining's binary_logloss: 0.232283\ttraining's auc: 0.938284\tvalid_1's binary_logloss: 0.268261\tvalid_1's auc: 0.884196\n",
      "[83]\ttraining's binary_logloss: 0.231603\ttraining's auc: 0.938585\tvalid_1's binary_logloss: 0.267913\tvalid_1's auc: 0.884321\n",
      "[84]\ttraining's binary_logloss: 0.230919\ttraining's auc: 0.938904\tvalid_1's binary_logloss: 0.26764\tvalid_1's auc: 0.884444\n",
      "[85]\ttraining's binary_logloss: 0.230258\ttraining's auc: 0.939117\tvalid_1's binary_logloss: 0.267388\tvalid_1's auc: 0.884415\n",
      "[86]\ttraining's binary_logloss: 0.229603\ttraining's auc: 0.939384\tvalid_1's binary_logloss: 0.26707\tvalid_1's auc: 0.884544\n",
      "[87]\ttraining's binary_logloss: 0.228996\ttraining's auc: 0.939644\tvalid_1's binary_logloss: 0.266821\tvalid_1's auc: 0.884586\n",
      "[88]\ttraining's binary_logloss: 0.228465\ttraining's auc: 0.939781\tvalid_1's binary_logloss: 0.266552\tvalid_1's auc: 0.884665\n",
      "[89]\ttraining's binary_logloss: 0.227836\ttraining's auc: 0.940052\tvalid_1's binary_logloss: 0.266275\tvalid_1's auc: 0.884829\n",
      "[90]\ttraining's binary_logloss: 0.227218\ttraining's auc: 0.940272\tvalid_1's binary_logloss: 0.266084\tvalid_1's auc: 0.884823\n",
      "[91]\ttraining's binary_logloss: 0.226675\ttraining's auc: 0.940479\tvalid_1's binary_logloss: 0.26585\tvalid_1's auc: 0.884884\n",
      "[92]\ttraining's binary_logloss: 0.226061\ttraining's auc: 0.94077\tvalid_1's binary_logloss: 0.265617\tvalid_1's auc: 0.884969\n",
      "[93]\ttraining's binary_logloss: 0.22549\ttraining's auc: 0.94099\tvalid_1's binary_logloss: 0.265452\tvalid_1's auc: 0.884928\n",
      "[94]\ttraining's binary_logloss: 0.224944\ttraining's auc: 0.941218\tvalid_1's binary_logloss: 0.265205\tvalid_1's auc: 0.885023\n",
      "[95]\ttraining's binary_logloss: 0.22434\ttraining's auc: 0.941492\tvalid_1's binary_logloss: 0.265027\tvalid_1's auc: 0.885002\n",
      "[96]\ttraining's binary_logloss: 0.223788\ttraining's auc: 0.941733\tvalid_1's binary_logloss: 0.264817\tvalid_1's auc: 0.885077\n",
      "[97]\ttraining's binary_logloss: 0.223149\ttraining's auc: 0.941999\tvalid_1's binary_logloss: 0.264632\tvalid_1's auc: 0.885108\n",
      "[98]\ttraining's binary_logloss: 0.222654\ttraining's auc: 0.942159\tvalid_1's binary_logloss: 0.264431\tvalid_1's auc: 0.885155\n",
      "[99]\ttraining's binary_logloss: 0.222054\ttraining's auc: 0.942509\tvalid_1's binary_logloss: 0.264207\tvalid_1's auc: 0.88532\n",
      "[100]\ttraining's binary_logloss: 0.221503\ttraining's auc: 0.942725\tvalid_1's binary_logloss: 0.264042\tvalid_1's auc: 0.88529\n",
      "[101]\ttraining's binary_logloss: 0.220973\ttraining's auc: 0.942985\tvalid_1's binary_logloss: 0.263865\tvalid_1's auc: 0.885299\n",
      "[102]\ttraining's binary_logloss: 0.220501\ttraining's auc: 0.943189\tvalid_1's binary_logloss: 0.263706\tvalid_1's auc: 0.885309\n",
      "[103]\ttraining's binary_logloss: 0.219961\ttraining's auc: 0.943433\tvalid_1's binary_logloss: 0.263527\tvalid_1's auc: 0.885403\n",
      "[104]\ttraining's binary_logloss: 0.219396\ttraining's auc: 0.943684\tvalid_1's binary_logloss: 0.263382\tvalid_1's auc: 0.885395\n",
      "[105]\ttraining's binary_logloss: 0.218841\ttraining's auc: 0.943984\tvalid_1's binary_logloss: 0.26322\tvalid_1's auc: 0.885461\n",
      "[106]\ttraining's binary_logloss: 0.218351\ttraining's auc: 0.944178\tvalid_1's binary_logloss: 0.263054\tvalid_1's auc: 0.88551\n",
      "[107]\ttraining's binary_logloss: 0.217819\ttraining's auc: 0.944443\tvalid_1's binary_logloss: 0.262911\tvalid_1's auc: 0.885516\n",
      "[108]\ttraining's binary_logloss: 0.217291\ttraining's auc: 0.944736\tvalid_1's binary_logloss: 0.262741\tvalid_1's auc: 0.885573\n",
      "[109]\ttraining's binary_logloss: 0.216793\ttraining's auc: 0.945016\tvalid_1's binary_logloss: 0.262603\tvalid_1's auc: 0.88561\n",
      "[110]\ttraining's binary_logloss: 0.216336\ttraining's auc: 0.945275\tvalid_1's binary_logloss: 0.262472\tvalid_1's auc: 0.885637\n",
      "[111]\ttraining's binary_logloss: 0.215933\ttraining's auc: 0.945447\tvalid_1's binary_logloss: 0.26236\tvalid_1's auc: 0.885607\n",
      "[112]\ttraining's binary_logloss: 0.215484\ttraining's auc: 0.945639\tvalid_1's binary_logloss: 0.262234\tvalid_1's auc: 0.885628\n",
      "[113]\ttraining's binary_logloss: 0.215021\ttraining's auc: 0.945844\tvalid_1's binary_logloss: 0.262085\tvalid_1's auc: 0.885664\n",
      "[114]\ttraining's binary_logloss: 0.214581\ttraining's auc: 0.946008\tvalid_1's binary_logloss: 0.261988\tvalid_1's auc: 0.885689\n",
      "[115]\ttraining's binary_logloss: 0.214139\ttraining's auc: 0.946172\tvalid_1's binary_logloss: 0.261822\tvalid_1's auc: 0.88575\n",
      "[116]\ttraining's binary_logloss: 0.213679\ttraining's auc: 0.946356\tvalid_1's binary_logloss: 0.261646\tvalid_1's auc: 0.885863\n",
      "[117]\ttraining's binary_logloss: 0.213245\ttraining's auc: 0.946567\tvalid_1's binary_logloss: 0.261525\tvalid_1's auc: 0.885899\n",
      "[118]\ttraining's binary_logloss: 0.212759\ttraining's auc: 0.946805\tvalid_1's binary_logloss: 0.261428\tvalid_1's auc: 0.885887\n",
      "[119]\ttraining's binary_logloss: 0.212365\ttraining's auc: 0.946969\tvalid_1's binary_logloss: 0.261301\tvalid_1's auc: 0.885955\n",
      "[120]\ttraining's binary_logloss: 0.211832\ttraining's auc: 0.947254\tvalid_1's binary_logloss: 0.261081\tvalid_1's auc: 0.88611\n",
      "[121]\ttraining's binary_logloss: 0.21139\ttraining's auc: 0.947478\tvalid_1's binary_logloss: 0.260989\tvalid_1's auc: 0.886136\n",
      "[122]\ttraining's binary_logloss: 0.210883\ttraining's auc: 0.947744\tvalid_1's binary_logloss: 0.26087\tvalid_1's auc: 0.886133\n",
      "[123]\ttraining's binary_logloss: 0.21047\ttraining's auc: 0.947942\tvalid_1's binary_logloss: 0.260759\tvalid_1's auc: 0.886172\n",
      "[124]\ttraining's binary_logloss: 0.20994\ttraining's auc: 0.948232\tvalid_1's binary_logloss: 0.260695\tvalid_1's auc: 0.886162\n",
      "[125]\ttraining's binary_logloss: 0.209568\ttraining's auc: 0.948386\tvalid_1's binary_logloss: 0.260564\tvalid_1's auc: 0.886241\n",
      "[126]\ttraining's binary_logloss: 0.209113\ttraining's auc: 0.948606\tvalid_1's binary_logloss: 0.260431\tvalid_1's auc: 0.886307\n",
      "[127]\ttraining's binary_logloss: 0.208646\ttraining's auc: 0.948846\tvalid_1's binary_logloss: 0.26028\tvalid_1's auc: 0.886381\n",
      "[128]\ttraining's binary_logloss: 0.208279\ttraining's auc: 0.949033\tvalid_1's binary_logloss: 0.260164\tvalid_1's auc: 0.886448\n",
      "[129]\ttraining's binary_logloss: 0.207845\ttraining's auc: 0.949258\tvalid_1's binary_logloss: 0.260091\tvalid_1's auc: 0.886466\n",
      "[130]\ttraining's binary_logloss: 0.207506\ttraining's auc: 0.949403\tvalid_1's binary_logloss: 0.259971\tvalid_1's auc: 0.8865\n",
      "[131]\ttraining's binary_logloss: 0.207084\ttraining's auc: 0.9496\tvalid_1's binary_logloss: 0.259903\tvalid_1's auc: 0.8865\n",
      "[132]\ttraining's binary_logloss: 0.206744\ttraining's auc: 0.949718\tvalid_1's binary_logloss: 0.259808\tvalid_1's auc: 0.886535\n",
      "[133]\ttraining's binary_logloss: 0.206395\ttraining's auc: 0.949867\tvalid_1's binary_logloss: 0.259762\tvalid_1's auc: 0.886503\n",
      "[134]\ttraining's binary_logloss: 0.20593\ttraining's auc: 0.950128\tvalid_1's binary_logloss: 0.259625\tvalid_1's auc: 0.886574\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[135]\ttraining's binary_logloss: 0.205496\ttraining's auc: 0.950358\tvalid_1's binary_logloss: 0.259561\tvalid_1's auc: 0.886553\n",
      "[136]\ttraining's binary_logloss: 0.205134\ttraining's auc: 0.950535\tvalid_1's binary_logloss: 0.259504\tvalid_1's auc: 0.886576\n",
      "[137]\ttraining's binary_logloss: 0.204866\ttraining's auc: 0.950654\tvalid_1's binary_logloss: 0.259396\tvalid_1's auc: 0.88666\n",
      "[138]\ttraining's binary_logloss: 0.204396\ttraining's auc: 0.950936\tvalid_1's binary_logloss: 0.259317\tvalid_1's auc: 0.886693\n",
      "[139]\ttraining's binary_logloss: 0.203922\ttraining's auc: 0.951224\tvalid_1's binary_logloss: 0.259251\tvalid_1's auc: 0.886688\n",
      "[140]\ttraining's binary_logloss: 0.203627\ttraining's auc: 0.951332\tvalid_1's binary_logloss: 0.259215\tvalid_1's auc: 0.886681\n",
      "[141]\ttraining's binary_logloss: 0.203365\ttraining's auc: 0.951405\tvalid_1's binary_logloss: 0.259191\tvalid_1's auc: 0.886687\n",
      "[142]\ttraining's binary_logloss: 0.20312\ttraining's auc: 0.951497\tvalid_1's binary_logloss: 0.25914\tvalid_1's auc: 0.886694\n",
      "[143]\ttraining's binary_logloss: 0.202781\ttraining's auc: 0.95167\tvalid_1's binary_logloss: 0.259089\tvalid_1's auc: 0.88668\n",
      "[144]\ttraining's binary_logloss: 0.202419\ttraining's auc: 0.951873\tvalid_1's binary_logloss: 0.259089\tvalid_1's auc: 0.886655\n",
      "[145]\ttraining's binary_logloss: 0.202104\ttraining's auc: 0.952027\tvalid_1's binary_logloss: 0.259063\tvalid_1's auc: 0.886616\n",
      "[146]\ttraining's binary_logloss: 0.201783\ttraining's auc: 0.952188\tvalid_1's binary_logloss: 0.259\tvalid_1's auc: 0.886647\n",
      "[147]\ttraining's binary_logloss: 0.201405\ttraining's auc: 0.952432\tvalid_1's binary_logloss: 0.258979\tvalid_1's auc: 0.886623\n",
      "[148]\ttraining's binary_logloss: 0.201017\ttraining's auc: 0.952671\tvalid_1's binary_logloss: 0.258957\tvalid_1's auc: 0.88665\n",
      "[149]\ttraining's binary_logloss: 0.200695\ttraining's auc: 0.952818\tvalid_1's binary_logloss: 0.25886\tvalid_1's auc: 0.886684\n",
      "[150]\ttraining's binary_logloss: 0.200428\ttraining's auc: 0.952956\tvalid_1's binary_logloss: 0.258766\tvalid_1's auc: 0.886734\n",
      "[151]\ttraining's binary_logloss: 0.200234\ttraining's auc: 0.953032\tvalid_1's binary_logloss: 0.258718\tvalid_1's auc: 0.886757\n",
      "[152]\ttraining's binary_logloss: 0.199894\ttraining's auc: 0.95322\tvalid_1's binary_logloss: 0.258706\tvalid_1's auc: 0.886703\n",
      "[153]\ttraining's binary_logloss: 0.199704\ttraining's auc: 0.953314\tvalid_1's binary_logloss: 0.258685\tvalid_1's auc: 0.886707\n",
      "[154]\ttraining's binary_logloss: 0.199416\ttraining's auc: 0.953463\tvalid_1's binary_logloss: 0.258676\tvalid_1's auc: 0.886691\n",
      "[155]\ttraining's binary_logloss: 0.199208\ttraining's auc: 0.953558\tvalid_1's binary_logloss: 0.258657\tvalid_1's auc: 0.886686\n",
      "[156]\ttraining's binary_logloss: 0.198893\ttraining's auc: 0.953711\tvalid_1's binary_logloss: 0.258594\tvalid_1's auc: 0.886711\n",
      "[157]\ttraining's binary_logloss: 0.198454\ttraining's auc: 0.953992\tvalid_1's binary_logloss: 0.258532\tvalid_1's auc: 0.886752\n",
      "[158]\ttraining's binary_logloss: 0.198234\ttraining's auc: 0.954093\tvalid_1's binary_logloss: 0.258499\tvalid_1's auc: 0.886777\n",
      "[159]\ttraining's binary_logloss: 0.197964\ttraining's auc: 0.954222\tvalid_1's binary_logloss: 0.258443\tvalid_1's auc: 0.886845\n",
      "[160]\ttraining's binary_logloss: 0.197706\ttraining's auc: 0.954381\tvalid_1's binary_logloss: 0.258384\tvalid_1's auc: 0.886872\n",
      "[161]\ttraining's binary_logloss: 0.197355\ttraining's auc: 0.954603\tvalid_1's binary_logloss: 0.258352\tvalid_1's auc: 0.886883\n",
      "[162]\ttraining's binary_logloss: 0.197081\ttraining's auc: 0.954722\tvalid_1's binary_logloss: 0.258306\tvalid_1's auc: 0.886903\n",
      "[163]\ttraining's binary_logloss: 0.19684\ttraining's auc: 0.95482\tvalid_1's binary_logloss: 0.258275\tvalid_1's auc: 0.886937\n",
      "[164]\ttraining's binary_logloss: 0.196633\ttraining's auc: 0.954916\tvalid_1's binary_logloss: 0.258238\tvalid_1's auc: 0.886963\n",
      "[165]\ttraining's binary_logloss: 0.196457\ttraining's auc: 0.954982\tvalid_1's binary_logloss: 0.258211\tvalid_1's auc: 0.886996\n",
      "[166]\ttraining's binary_logloss: 0.196263\ttraining's auc: 0.955093\tvalid_1's binary_logloss: 0.258186\tvalid_1's auc: 0.887022\n",
      "[167]\ttraining's binary_logloss: 0.196097\ttraining's auc: 0.955152\tvalid_1's binary_logloss: 0.258151\tvalid_1's auc: 0.88703\n",
      "[168]\ttraining's binary_logloss: 0.195867\ttraining's auc: 0.955272\tvalid_1's binary_logloss: 0.258115\tvalid_1's auc: 0.887004\n",
      "[169]\ttraining's binary_logloss: 0.195574\ttraining's auc: 0.95542\tvalid_1's binary_logloss: 0.258043\tvalid_1's auc: 0.887074\n",
      "[170]\ttraining's binary_logloss: 0.195416\ttraining's auc: 0.955485\tvalid_1's binary_logloss: 0.258017\tvalid_1's auc: 0.887074\n",
      "[171]\ttraining's binary_logloss: 0.195244\ttraining's auc: 0.955573\tvalid_1's binary_logloss: 0.258002\tvalid_1's auc: 0.887086\n",
      "[172]\ttraining's binary_logloss: 0.19507\ttraining's auc: 0.95567\tvalid_1's binary_logloss: 0.257947\tvalid_1's auc: 0.887114\n",
      "[173]\ttraining's binary_logloss: 0.194869\ttraining's auc: 0.955762\tvalid_1's binary_logloss: 0.257918\tvalid_1's auc: 0.887141\n",
      "[174]\ttraining's binary_logloss: 0.194719\ttraining's auc: 0.955825\tvalid_1's binary_logloss: 0.257877\tvalid_1's auc: 0.887161\n",
      "[175]\ttraining's binary_logloss: 0.19457\ttraining's auc: 0.955892\tvalid_1's binary_logloss: 0.257826\tvalid_1's auc: 0.887214\n",
      "[176]\ttraining's binary_logloss: 0.194249\ttraining's auc: 0.956112\tvalid_1's binary_logloss: 0.257841\tvalid_1's auc: 0.887163\n",
      "[177]\ttraining's binary_logloss: 0.194093\ttraining's auc: 0.956176\tvalid_1's binary_logloss: 0.257808\tvalid_1's auc: 0.887191\n",
      "[178]\ttraining's binary_logloss: 0.193891\ttraining's auc: 0.956304\tvalid_1's binary_logloss: 0.257768\tvalid_1's auc: 0.887223\n",
      "[179]\ttraining's binary_logloss: 0.193542\ttraining's auc: 0.956516\tvalid_1's binary_logloss: 0.257768\tvalid_1's auc: 0.887189\n",
      "[180]\ttraining's binary_logloss: 0.193332\ttraining's auc: 0.956613\tvalid_1's binary_logloss: 0.257727\tvalid_1's auc: 0.887226\n",
      "[181]\ttraining's binary_logloss: 0.1932\ttraining's auc: 0.956668\tvalid_1's binary_logloss: 0.257699\tvalid_1's auc: 0.887227\n",
      "[182]\ttraining's binary_logloss: 0.193003\ttraining's auc: 0.956745\tvalid_1's binary_logloss: 0.257665\tvalid_1's auc: 0.887237\n",
      "[183]\ttraining's binary_logloss: 0.192873\ttraining's auc: 0.956803\tvalid_1's binary_logloss: 0.25764\tvalid_1's auc: 0.887245\n",
      "[184]\ttraining's binary_logloss: 0.192665\ttraining's auc: 0.956891\tvalid_1's binary_logloss: 0.25761\tvalid_1's auc: 0.887247\n",
      "[185]\ttraining's binary_logloss: 0.192362\ttraining's auc: 0.957066\tvalid_1's binary_logloss: 0.257622\tvalid_1's auc: 0.887222\n",
      "[186]\ttraining's binary_logloss: 0.192156\ttraining's auc: 0.95719\tvalid_1's binary_logloss: 0.257652\tvalid_1's auc: 0.887209\n",
      "[187]\ttraining's binary_logloss: 0.192011\ttraining's auc: 0.957259\tvalid_1's binary_logloss: 0.257632\tvalid_1's auc: 0.88723\n",
      "[188]\ttraining's binary_logloss: 0.191686\ttraining's auc: 0.957462\tvalid_1's binary_logloss: 0.257625\tvalid_1's auc: 0.887222\n",
      "[189]\ttraining's binary_logloss: 0.191531\ttraining's auc: 0.957548\tvalid_1's binary_logloss: 0.257615\tvalid_1's auc: 0.887212\n",
      "[190]\ttraining's binary_logloss: 0.191361\ttraining's auc: 0.957619\tvalid_1's binary_logloss: 0.2576\tvalid_1's auc: 0.887236\n",
      "[191]\ttraining's binary_logloss: 0.191234\ttraining's auc: 0.957689\tvalid_1's binary_logloss: 0.257584\tvalid_1's auc: 0.887255\n",
      "[192]\ttraining's binary_logloss: 0.191021\ttraining's auc: 0.957784\tvalid_1's binary_logloss: 0.257503\tvalid_1's auc: 0.887325\n",
      "[193]\ttraining's binary_logloss: 0.190788\ttraining's auc: 0.957916\tvalid_1's binary_logloss: 0.257549\tvalid_1's auc: 0.887251\n",
      "[194]\ttraining's binary_logloss: 0.190674\ttraining's auc: 0.957985\tvalid_1's binary_logloss: 0.257558\tvalid_1's auc: 0.887252\n",
      "[195]\ttraining's binary_logloss: 0.190572\ttraining's auc: 0.958025\tvalid_1's binary_logloss: 0.257521\tvalid_1's auc: 0.887288\n",
      "[196]\ttraining's binary_logloss: 0.190405\ttraining's auc: 0.95811\tvalid_1's binary_logloss: 0.257513\tvalid_1's auc: 0.887268\n",
      "[197]\ttraining's binary_logloss: 0.19028\ttraining's auc: 0.958172\tvalid_1's binary_logloss: 0.257526\tvalid_1's auc: 0.887259\n",
      "[198]\ttraining's binary_logloss: 0.190048\ttraining's auc: 0.958295\tvalid_1's binary_logloss: 0.257496\tvalid_1's auc: 0.887294\n",
      "[199]\ttraining's binary_logloss: 0.189939\ttraining's auc: 0.95834\tvalid_1's binary_logloss: 0.257427\tvalid_1's auc: 0.887349\n",
      "[200]\ttraining's binary_logloss: 0.189859\ttraining's auc: 0.95838\tvalid_1's binary_logloss: 0.257434\tvalid_1's auc: 0.887347\n",
      "[201]\ttraining's binary_logloss: 0.189624\ttraining's auc: 0.958523\tvalid_1's binary_logloss: 0.257385\tvalid_1's auc: 0.887399\n",
      "[202]\ttraining's binary_logloss: 0.189413\ttraining's auc: 0.958654\tvalid_1's binary_logloss: 0.25735\tvalid_1's auc: 0.887421\n",
      "[203]\ttraining's binary_logloss: 0.189261\ttraining's auc: 0.958717\tvalid_1's binary_logloss: 0.257316\tvalid_1's auc: 0.887412\n",
      "[204]\ttraining's binary_logloss: 0.189161\ttraining's auc: 0.958762\tvalid_1's binary_logloss: 0.257286\tvalid_1's auc: 0.88745\n",
      "[205]\ttraining's binary_logloss: 0.188932\ttraining's auc: 0.958924\tvalid_1's binary_logloss: 0.25727\tvalid_1's auc: 0.887487\n",
      "[206]\ttraining's binary_logloss: 0.188877\ttraining's auc: 0.958947\tvalid_1's binary_logloss: 0.257274\tvalid_1's auc: 0.887499\n",
      "[207]\ttraining's binary_logloss: 0.188747\ttraining's auc: 0.959009\tvalid_1's binary_logloss: 0.257211\tvalid_1's auc: 0.887553\n",
      "[208]\ttraining's binary_logloss: 0.188691\ttraining's auc: 0.959026\tvalid_1's binary_logloss: 0.257212\tvalid_1's auc: 0.887549\n",
      "[209]\ttraining's binary_logloss: 0.188523\ttraining's auc: 0.959105\tvalid_1's binary_logloss: 0.257186\tvalid_1's auc: 0.887559\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[210]\ttraining's binary_logloss: 0.188424\ttraining's auc: 0.959147\tvalid_1's binary_logloss: 0.257153\tvalid_1's auc: 0.887612\n",
      "[211]\ttraining's binary_logloss: 0.18829\ttraining's auc: 0.959202\tvalid_1's binary_logloss: 0.257136\tvalid_1's auc: 0.887613\n",
      "[212]\ttraining's binary_logloss: 0.188115\ttraining's auc: 0.959277\tvalid_1's binary_logloss: 0.257106\tvalid_1's auc: 0.887638\n",
      "[213]\ttraining's binary_logloss: 0.188065\ttraining's auc: 0.959292\tvalid_1's binary_logloss: 0.257119\tvalid_1's auc: 0.887634\n",
      "[214]\ttraining's binary_logloss: 0.187946\ttraining's auc: 0.959342\tvalid_1's binary_logloss: 0.257076\tvalid_1's auc: 0.887662\n",
      "[215]\ttraining's binary_logloss: 0.187804\ttraining's auc: 0.9594\tvalid_1's binary_logloss: 0.257024\tvalid_1's auc: 0.887727\n",
      "[216]\ttraining's binary_logloss: 0.18752\ttraining's auc: 0.959595\tvalid_1's binary_logloss: 0.25708\tvalid_1's auc: 0.887674\n",
      "[217]\ttraining's binary_logloss: 0.187453\ttraining's auc: 0.959629\tvalid_1's binary_logloss: 0.257083\tvalid_1's auc: 0.887682\n",
      "[218]\ttraining's binary_logloss: 0.187369\ttraining's auc: 0.959666\tvalid_1's binary_logloss: 0.257068\tvalid_1's auc: 0.887699\n",
      "[219]\ttraining's binary_logloss: 0.187285\ttraining's auc: 0.95971\tvalid_1's binary_logloss: 0.257076\tvalid_1's auc: 0.887704\n",
      "[220]\ttraining's binary_logloss: 0.18701\ttraining's auc: 0.959886\tvalid_1's binary_logloss: 0.257107\tvalid_1's auc: 0.887669\n",
      "[221]\ttraining's binary_logloss: 0.186868\ttraining's auc: 0.959955\tvalid_1's binary_logloss: 0.257104\tvalid_1's auc: 0.887646\n",
      "[222]\ttraining's binary_logloss: 0.18674\ttraining's auc: 0.960014\tvalid_1's binary_logloss: 0.257098\tvalid_1's auc: 0.88766\n",
      "[223]\ttraining's binary_logloss: 0.186447\ttraining's auc: 0.96021\tvalid_1's binary_logloss: 0.257117\tvalid_1's auc: 0.887615\n",
      "[224]\ttraining's binary_logloss: 0.186349\ttraining's auc: 0.960267\tvalid_1's binary_logloss: 0.257138\tvalid_1's auc: 0.887589\n",
      "[225]\ttraining's binary_logloss: 0.186175\ttraining's auc: 0.960383\tvalid_1's binary_logloss: 0.257154\tvalid_1's auc: 0.887573\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['best_model.pkl']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 划分训练集与验证集\n",
    "x_train, x_valid, y_train, y_valid = train_test_split(X_transformed, y, test_size=0.3, random_state=12)\n",
    "\n",
    "# 得到贝叶斯搜索得到的最佳参数\n",
    "best_params = bayes_search.best_params_\n",
    "\n",
    "# 将最佳参数设置到LightGBM模型中\n",
    "lgbm_model.set_params(**best_params)\n",
    "\n",
    "# 训练模型\n",
    "lgbm_model.fit(x_train, y_train, \n",
    "               eval_set=[(x_train, y_train), (x_valid, y_valid)], \n",
    "               eval_metric=['binary_logloss', 'auc'], \n",
    "               early_stopping_rounds=10)\n",
    "joblib.dump(lgbm_model, \"best_model.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4628729d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T14:52:32.971181Z",
     "start_time": "2023-11-19T14:52:32.952710Z"
    }
   },
   "outputs": [],
   "source": [
    "# 获取当前时间戳\n",
    "timestamp = time.time()\n",
    "\n",
    "# 将时间戳转换为 struct_time 对象\n",
    "struct_time = time.localtime(timestamp)\n",
    "\n",
    "best_score = lgbm_model.best_score_\n",
    "score_dict = {\n",
    "    time.strftime('%Y-%m-%d', struct_time): {\n",
    "        \"TRAIN_AUC\": best_score[\"training\"][\"auc\"],\n",
    "        \"TRAIN_LOSS\": best_score[\"training\"][\"binary_logloss\"],\n",
    "        \"VALID_AUC\": best_score[\"valid_1\"][\"auc\"],\n",
    "        \"VALID_LOSS\": best_score[\"valid_1\"][\"binary_logloss\"]\n",
    "    }\n",
    "}\n",
    "\n",
    "with open(\"../logging/best_score_log.json\", \"w\") as json_file:\n",
    "    json.dump(score_dict, json_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c1f698bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T14:55:02.950716Z",
     "start_time": "2023-11-19T14:55:02.930089Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"2023-11-19 22:52:32 best_iteration=215 best_score=defaultdict(<class 'collections.OrderedDict'>, {'training': OrderedDict([('binary_logloss', 0.1878041416622663), ('auc', 0.9594001455201256)]), 'valid_1': OrderedDict([('binary_logloss', 0.2570238353747708), ('auc', 0.8877268814589666)])})\""
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_iteration = lgbm_model.best_iteration_\n",
    "# 将 struct_time 对象格式化为字符串\n",
    "formatted_date = time.strftime('%Y-%m-%d %H:%M:%S', struct_time)\n",
    "log = f\"{formatted_date} best_iteration={best_iteration} best_score={best_score}\"\n",
    "\n",
    "# 配置日志\n",
    "logging.basicConfig(filename='../logging/model_train_logging.log', level=logging.INFO)\n",
    "\n",
    "logging.info(log)\n",
    "log"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4f58ffe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-18T11:26:58.102173Z",
     "start_time": "2023-11-18T11:26:58.086017Z"
    }
   },
   "source": [
    "# 使用模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c66d1743",
   "metadata": {},
   "source": [
    "模型得分：686/0.96"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "84bf41e2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-19T14:55:23.562525Z",
     "start_time": "2023-11-19T14:55:23.413191Z"
    }
   },
   "outputs": [],
   "source": [
    "test = pd.read_csv(\"../dataset/test.csv\")\n",
    "use_test = test.iloc[:, 1:]\n",
    "\n",
    "test_transformed = preprocessing_pipeline.transform(use_test)\n",
    "test[\"subscribe\"] = lgbm_model.predict(test_transformed)\n",
    "test[\"subscribe\"] = test[\"subscribe\"].apply(lambda x: \"yes\" if x == 1 else \"no\")\n",
    "\n",
    "test[[\"id\", \"subscribe\"]].to_csv(\"../results/submission_v1.3.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "611d34d1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-18T14:18:31.128587Z",
     "start_time": "2023-11-18T14:18:31.123572Z"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "887850f9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ML",
   "language": "python",
   "name": "ml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "264.875px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
